package com.ca.ui.core.util;

import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class PaperUtil {

    public static String parsePaperForText(String paperContext) {

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            byte[] buffer = paperContext.getBytes(StandardCharsets.UTF_8);
            ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
            Document document = builder.parse(bis);

            Element paperElement = document.getDocumentElement();
            NodeList nodes = paperElement.getChildNodes();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node pn = nodes.item(i);
                NodeList pnNodes = pn.getChildNodes();
                for (int j = 0; j < pnNodes.getLength(); j++) {
                    Node node = pnNodes.item(j);
                    String nodeName = node.getNodeName();
                    if ("text".equals(nodeName)) {
                        sb.append(node.getTextContent()).append(" ");
                    }
                }
            }

            return sb.toString();
        }
        catch (Exception e) {
            return "";
        }
    }

    public static String[] parsePaperImageUrlArray(String paperContext) {

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            byte[] buffer = paperContext.getBytes(StandardCharsets.UTF_8);
            ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
            Document document = builder.parse(bis);

            Element paperElement = document.getDocumentElement();
            NodeList nodes = paperElement.getChildNodes();

            List<String> imageNetPathList = new ArrayList<>();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node pn = nodes.item(i);
                NodeList pnNodes = pn.getChildNodes();
                for (int j = 0; j < pnNodes.getLength(); j++) {
                    Node node = pnNodes.item(j);
                    String nodeName = node.getNodeName();
                    if ("image".equals(nodeName)) {
                        String path = node.getTextContent().replaceAll("&amp;", "&");
                        imageNetPathList.add(path);
                    }
                }
            }

            String[] array = new String[imageNetPathList.size()];
            for (int i = 0; i < array.length; i++) {
                array[i] = imageNetPathList.get(i);
            }
            return array;
        }
        catch (Exception e) {
            return new String[0];
        }
    }
}
